﻿/*
 * Nuget包：Microsoft.Extensions.Configuration.Json
 * 
 * **/
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Max.PH5_2006.ShoppingSite.Common.Pay
{
    /// <summary>
    /// 支付宝的参数配置类
    /// </summary>
    public sealed class AliPayConfig
    {

        /// <summary>
        /// 应用APPID
        /// </summary>
        public string? AppId { get; set; }

        /// <summary>
        /// 支付网关URL
        /// </summary>
        public string? GatewayUrl { get; set; }

        /// <summary>
        /// 商户私钥
        /// </summary>
        public string? PrivateKey { get; set; }

        /// <summary>
        /// 支付宝公钥
        /// </summary>
        public string? AlipayPublicKey { get; set; }

        /// <summary>
        /// 签名方式
        /// </summary>
        public string? SignType { get; set; }

        /// <summary>
        /// 编码格式
        /// </summary>
        public string? Charset { get; set; }


        readonly IConfiguration _configuration;

        // 采用读取配置文件
        public AliPayConfig()
        {
            //采用这种方式，需要安装包：Microsoft.Extensions.Configuration.Json
            //ReloadOnChange = true表示appsetting.json文件发生改变后，会重新加载
            _configuration = new ConfigurationBuilder()
                 .Add(new JsonConfigurationSource { Path = "appsettings.json", ReloadOnChange = true })
                 .Build();

            this.AppId = _configuration.GetSection("AlipayConfig:APPID").Value;
            this.GatewayUrl = _configuration["AlipayConfig:GatewayUrl"];
            this.PrivateKey = _configuration["AlipayConfig:PrivateKey"];
            this.AlipayPublicKey = _configuration["AlipayConfig:AlipayPublicKey"];
            this.SignType = _configuration["AlipayConfig:SignType"];
            this.Charset = _configuration["AlipayConfig:Charset"];
        }

        //采用注入IConfiguration对象来获取配置
        //public AliPayConfigHelper(IConfiguration configuration)
        //{
        //    _configuration = configuration;
        //    this.AppId = _configuration.GetSection("AlipayConfig:APPID").Value;
        //    this.GatewayUrl = _configuration["AlipayConfig:GatewayUrl"];
        //    this.PrivateKey = _configuration["AlipayConfig:PrivateKey"];
        //    this.AlipayPublicKey = _configuration["AlipayConfig:AlipayPublicKey"];
        //    this.SignType = _configuration["AlipayConfig:SignType"];
        //    this.Charset = _configuration["AlipayConfig:Charset"];
        //}
    }
}
